package com.zxt.system;

import com.zxt.bean.AjaxResult;
import com.zxt.constant.Constants;
import com.zxt.dto.LoginBodyDTO;
import com.zxt.security.bean.SysUser;
import com.zxt.security.utils.SecurityUtils;
import com.zxt.user.service.SysLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录验证
 *
 * @author zxt
 */
@RestController
@Api(tags = "登录")
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;


    /**
     * 登录方法
     *
     * @param dto 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "登录")
    public AjaxResult login(@RequestBody LoginBodyDTO dto) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(dto.getUsername(), dto.getPassword(), dto.getCode(),
                dto.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        return ajax;
    }

}
